import { Component, Input } from '@angular/core';
import { NavController, NavParams, ToastController, LoadingController } from 'ionic-angular';
import { BaseUI } from '../../common/baseui';
import { Storage } from '@ionic/storage';
import { RestProvider } from '../../providers/rest/rest';
import { DetailsPage } from '../../pages/details/details';



@Component({
  selector: 'question-list',
  templateUrl: 'question-list.html'
})
export class QuestionListComponent extends BaseUI {

  errorMessage: any;
  questions: string[];
  //datatype 外部传递进来的,dataSourceType 本地接收之后的参数命名
  @Input('datatype') dataSourceType;
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private toastCtrl: ToastController,
    private loadingCtrl: LoadingController,
    private storage: Storage,
    private rest: RestProvider
  ) {
    super();
  }

  //组件这里没有声明周期的函数
  ngAfterContentInit() {
      this.storage.get('UserId').then((val) => {
      if (val != null) {
        //加载用户数据
        var loading = super.showLoading(this.loadingCtrl, "加载中...");
        this.rest.getUserQuestionList(val,this.dataSourceType)
          .subscribe(data => {
            this.questions = data;
            loading.dismissAll();
          }, error => this.errorMessage = <any>error);
      }
    })
  }

 gotoDetaild(id) {
    this.navCtrl.push(DetailsPage, { id: id });
  }
}
